package com.nciae.service;

import com.nciae.config.ElasticSearchConfigurationProperties;
import com.nciae.utils.HttpUtils;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Service
public class CymalElasticSearchService implements IElasticSearchService{

    @Autowired
    private ElasticSearchConfigurationProperties elasticSearchConfigurationProperties;

    /**
     * @param indexName：索引名
     * @return：返回索引库名
     */
    @Override
    public String createIndex(String indexName) throws IOException {
        // 创建HttpPut请求
        HttpPut httpPut = HttpUtils.getHttpPut(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName);
        httpPut.setEntity(new StringEntity("{\n" +
                "    \"settings\": {\n" +
                "        \"number_of_shards\": 3,\n" +
                "        \"number_of_replicas\": 2\n" +
                "    }\n" +
                "}"));
        return HttpUtils.excute(httpPut);
    }

    /**
     * 查看索引
     * @param indexName：索引
     * @return
     */
    @Override
    public String selectIndex(String indexName) throws IOException {
        HttpGet httpGet = HttpUtils.getHttpGet(elasticSearchConfigurationProperties.getHost()
                +":"
                +elasticSearchConfigurationProperties.getPort()
                +"/"
                +indexName);
        return HttpUtils.excute(httpGet);
    }

    /**
     * 删除索引
     * @param indexName：索引名
     * @return
     */
    @Override
    public String deleteIndex(String indexName) throws IOException {
        HttpDelete httpDelete = HttpUtils.getHttpDelete(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName);
        return HttpUtils.excute(httpDelete);
    }

    /**
     * 创建映射字段
     * @param indexName：索引库名
     * @param typeName：类型名
     * @param body：方法体名
     * @return
     */
    @Override
    public String createMapping(String indexName, String typeName, String body) throws Exception {
        HttpPut httpPut = HttpUtils.getHttpPut(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/_mapping/"
                + typeName);
        httpPut.setEntity(new StringEntity(body));
        return HttpUtils.excute(httpPut);
    }

    @Override
    public String selectMapping(String indexName) throws IOException {
        HttpGet httpGet = HttpUtils.getHttpGet(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/_mapping");
        return HttpUtils.excute(httpGet);
    }

    @Override
    public String postDocument(String indexName, String typeName, String body) throws IOException {
        HttpPost httpPost = HttpUtils.getHttpPost(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/"
                + typeName);
        httpPost.setEntity(new StringEntity(body));
        return HttpUtils.excute(httpPost);
    }

    @Override
    public String postDocumentAppointId(String indexName, String typeName, String id, String body) throws IOException {
        HttpPost httpPost = HttpUtils.getHttpPost(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/"
                + typeName
                +"/"
                +id);
        httpPost.setEntity(new StringEntity(body));
        return HttpUtils.excute(httpPost);
    }

    @Override
    public String putDocumentAppointId(String indexName, String typeName, String id, String body) throws IOException {
        HttpPut httpPut = HttpUtils.getHttpPut(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/"
                + typeName
                + "/"
                + id);
        httpPut.setEntity(new StringEntity(body));
        return HttpUtils.excute(httpPut);
    }

    @Override
    public String deleteDocumentAppointId(String indexName, String typeName, String id, String body) throws IOException {
        HttpDelete httpDelete = HttpUtils.getHttpDelete(elasticSearchConfigurationProperties.getHost()
                + ":"
                + elasticSearchConfigurationProperties.getPort()
                + "/"
                + indexName
                + "/"
                + typeName
                + "/"
                + id);
        return HttpUtils.excute(httpDelete);
    }

    @Override
    public String baseSelect(String indexName, String body) {
        return null;
    }

    @Override
    public String allSelect(String indexName) {
        return null;
    }
}